package com.xue.web.service;

import com.alibaba.fastjson.JSONObject;
import com.xue.web.dao.LoginDao;

import java.util.List;
import java.util.Map;

public class LoginService {
    //执行登录业务
    public JSONObject doLoging(String userName,String userPassword){
        System.out.println(this.getClass().getSimpleName()+"|执行登录业务");
        JSONObject jsonObject =new JSONObject();


        LoginDao loginDao =new LoginDao();
        Map<String, List<Object>> stringListMap = loginDao.selectUser(userName);
        if (stringListMap.size()==0){
            System.out.println(this.getClass().getSimpleName()+"|警告:查询失效");
            jsonObject.put("code",400);
            jsonObject.put("msg","没有这个用户");
            return jsonObject;
        }
        List<Object> userNameList = stringListMap.get("user_name");
        List<Object> userPasswordList = stringListMap.get("user_password");

        if (userNameList.size()!=1){
            if (userPasswordList.size()>1){
                throw  new RuntimeException(this.getClass().getSimpleName()+"|严重:重复查询到用户");
            }
            
            jsonObject.put("code",400);
            jsonObject.put("msg","没有这个用户");
            return jsonObject;
        }
        for (int i = 0; i < userNameList.size(); i++) {
            if (userNameList.get(i).equals(userName)){
                System.out.println(this.getClass().getSimpleName()+"|查询到用户存在");
                if (userPasswordList.get(i).equals(userPassword)){
                    System.out.println(this.getClass().getSimpleName()+"|用户密码匹配成功");

                    jsonObject.put("code",200);
                    jsonObject.put("msg","验证通过");
                    return jsonObject;
                }else {
                    jsonObject.put("code",400);
                    jsonObject.put("msg","秘密错误");
                    return jsonObject;
                }
            }
        }

        jsonObject.put("code",400);
        jsonObject.put("msg","没有这个用户");
        return jsonObject;
    }

}
